package com.mindai.esper.monitor.facade.handler;

import com.mindai.common.exception.BusinessException;
import com.mindai.common.model.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * 统一异常处理
 * <p>
 * Created by Ladd on 2016/10/21.
 */
@Slf4j
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultData exceptionHandler(Exception ex, HttpServletResponse response) throws Exception {
        ResultData rs = new ResultData();
        rs.setMsg("系统繁忙，请稍后再试！");
        if (ex instanceof BusinessException) {
            log.warn("requestId=" + UUID.randomUUID(), ex);
            BusinessException be = (BusinessException) ex;
            rs.setCode(be.getCode());
            rs.setMsg(be.getMessage());
        } else if (ex instanceof Exception) {
            log.error("requestId=" + UUID.randomUUID(), ex);
            //throw ex;
        }
        return rs;
    }
}
